<template>
  <div class="myContainer">
    <!-- <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">考核模板默认人员职务</el-col>
    </el-row> 

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        责任人职务 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="责任人职务" v-model="personLiable" @focus="getRole('personLiable','personLiableID')"></el-input>
      </el-col>
    </el-row> 

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        处理人职务 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="处理人职务" v-model="processingPerson" @focus="getRole('processingPerson','processingPersonID')"></el-input>
      </el-col>
    </el-row>  -->

    <el-row class="myBorderBottom myMarginBottom_10px">
      <el-col :span="7" class="myTitleText">整改任务默认人员职务</el-col>
    </el-row> 

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        责任人职务 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="责任人职务" v-model="charge_role_name_rectify" @focus="getRole('charge_role_name_rectify','charge_role_id_rectify')"></el-input>
      </el-col>
    </el-row> 

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="3" class="myLeftMsg">
        处理人职务 :
      </el-col>
      <el-col :span="6">
        <el-input placeholder="处理人职务" v-model="deal_role_name_rectify" @focus="getRole('deal_role_name_rectify','deal_role_id_rectify')"></el-input>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="myMarginBottom_10px">
      <el-col :span="6" :offset="3"> 
        <el-button type="primary" @click="update">保存</el-button>
      </el-col>
    </el-row>
    
    <!-- 选择职务 start -->
    <el-dialog title="选择职务" class="myRoleDialog" :visible.sync="newRoleDialogVisible">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-input placeholder="输入关键词" v-model="kw_role"></el-input>
        </el-col>
        <el-col :span="4" class="myTextAlignRight">
          <el-button type="primary" class="myBgGreen" @click="onSearchRole">查询</el-button>
        </el-col>
      </el-row>
      <div class="myPaddingTop_10px">
        <el-tree 
          key="roleDlg"
          class="myOV_y_400px"
          ref="roleDlg"
          v-loading="roleLoading"
          :data="roleDlgList"
          :props="roleDlgProps"
          node-key="role_id"
          @check-change="onChange"
          show-checkbox>
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="newRoleDialogVisible = false">取 消</el-button>
        <el-button @click="handleRoleClick" type="primary" class="myBgGreen">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 选择职务 end -->
  </div>
</template>     
  
<style scoped>
  .myBorderBottom{
    border-bottom:1px dotted #dbdbdb;
    padding-bottom: 5px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myred{
    color: #f00;
    font-size: 12px;
  }
  .myMarginBottom_10px{
    margin-bottom: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .mymarginTop_10px{
    margin-top: 10px;
  }
  .myOV_y_400px{
    border:1px solid #f2f2f2;
    box-sizing: border-box;
    min-height: 200px;
    max-height: 400px;
    overflow-y: scroll; 
  } 
</style>

<script>
  import { getRoleList } from '@/api/role';
  import { infoRule , updateRule } from '@/api/quality';
  export default {
    data(){
      return{
        title:"人员角色管理",
        type:"",
        id:"",
        readonly:false,
        radio:1,
        kw_role:"",
        newRoleDialogVisible:false,
        roleDlgList:[],
        roleLoading:false,
        roleIdList:"",
        roleNameList:"",
        roleDlgProps: {
          children: 'children',
          label: 'role_name'
        },
        personLiable:"",//主任务责任人
        personLiableID:"",//主任务责任人id
        processingPerson:"",//主任务处理人
        processingPersonID:"",//主任务处理人id

        charge_role_id_rectify:"",//整改责任人职务id
        charge_role_name_rectify:"",//整改责任人职务名称
        deal_role_id_rectify:"",//整改处理人职务id
        deal_role_name_rectify:"",//整改处理人职务名称

        that:"",
        thatID:"",
        i:0
      }
    },
    components: {
    },
    created(){
      document.title=this.title; 
      this.info();
    },
    computed: {
    },
    watch: { 
    },
    methods:{ 
      update(){

      },
      back(){
        this.$router.go(-1);
      },
      onGetRoleList(){//拉取角色职务
        let vm = this;
        vm.roleLoading = true;
        let params = {
          kw: vm.kw_role,
          page_num: 1,
          page_size: 10000
        }; 
        getRoleList(params).then(response => {
          let res = response.data;
          vm.roleDlgList = res.rows;
          vm.roleLoading = false;
        }).catch(error => {
          vm.roleLoading = false;
        })
      },
      onSearchRole(){//职务搜索
        this.onGetRoleList();
      },
      getRole(that,thatID){//职务框获得焦点时
        let vm=this; 
        vm.that=that;
        vm.thatID=thatID;
        vm.onGetRoleList();
        vm.newRoleDialogVisible=true; 
      },
      handleRoleClick(){//职务确认
        let vm=this; 
        let ids="",names="";
        let role = vm.$refs.roleDlg.getCheckedNodes(); 

        if(role.length>0){
          for (var i = 0; i < role.length ; i++) {
            ids+=role[i].role_id+",";
            names+=role[i].role_name+",";
          }
          vm[vm.thatID]=ids.substring(0,ids.length-1);
          vm[vm.that]=names.substring(0,names.length-1);

        }else{
          vm[vm.thatID]="";
          vm[vm.that]="";
        } 
        vm.newRoleDialogVisible=false;
      },
      onChange(a,b,c){
        let vm=this;
        vm.i++;
        if(vm.i%2==0){
          if(b){
            vm.$refs.roleDlg.setCheckedKeys([]);
            vm.$refs.roleDlg.setCheckedKeys([a.role_id]);
          }else{
            vm.$refs.roleDlg.setCheckedKeys([]);
          }
        }
      },
      info(){
        let vm=this;
        infoRule().then(response => {
          let res=response.data;

          vm.personLiableID=res.charge_role_id_task;//主任务责任人id
          vm.personLiable=res.charge_role_name_task;//主任务责任人名称
          vm.processingPersonID=res.deal_role_id_task;//主任务处理人id
          vm.processingPerson=res.deal_role_name_task;//主任务处理人名称
          
          vm.charge_role_id_rectify=res.charge_role_id_rectify;//整改任务责任人id
          vm.charge_role_name_rectify=res.charge_role_name_rectify;//整改任务责任人名称
          vm.deal_role_id_rectify=res.deal_role_id_rectify;//整改任务处理人id
          vm.deal_role_name_rectify=res.deal_role_name_rectify;//整改任务处理人名称
           
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      update(){
        let vm=this;
        let params={
          "charge_role_id_task": vm.personLiableID,
          "charge_role_name_task": vm.personLiable,
          "deal_role_id_task": vm.processingPersonID,
          "deal_role_name_task": vm.processingPerson,

          "charge_role_id_rectify": vm.charge_role_id_rectify,
          "charge_role_name_rectify": vm.charge_role_name_rectify,
          "deal_role_id_rectify": vm.deal_role_id_rectify,
          "deal_role_name_rectify": vm.deal_role_name_rectify,
        }
        updateRule(params).then(response => {
          vm.$message.success("执行成功！");
        }).catch(error => {
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      }
    }
}
</script>

